In 2011, Mexico was under the international spotlight for an unprecedented phenomenon: an increase in violence, especially reflected in the rise of homicides. The homicides trend has not been constant in the period of 2006 to 2015 and its the geographical distribution has changed over the years, reflecting the government strategies and the interaction of criminal groups in the country.
library(raster)
## Loading required package: sp
library(sp)
library(rgdal)
## rgdal: version: 1.2-16, (SVN revision 701)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 2.1.2, released 2016/10/24
## Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rgdal/gdal
## GDAL binary built with GEOS: FALSE
## Loaded PROJ.4 runtime: Rel. 4.9.1, 04 March 2015, [PJ_VERSION: 491]
## Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rgdal/proj
## Linking to sp version: 1.2-5
library(readr)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 2.2.1.9000 ✔ purrr 0.2.4
## ✔ tibble 1.4.1 ✔ dplyr 0.7.4
## ✔ tidyr 0.7.2 ✔ stringr 1.2.0
## ✔ ggplot2 2.2.1.9000 ✔ forcats 0.2.0
## ── Conflicts ────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ tidyr::extract() masks raster::extract()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::select() masks raster::select()
library(crosstalk)
library (plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:raster':
##
## select
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(dplyr)
library(ggplot2)
library(rasterVis)
## Loading required package: lattice
## Loading required package: latticeExtra
## Loading required package: RColorBrewer
##
## Attaching package: 'latticeExtra'
## The following object is masked from 'package:ggplot2':
##
## layer
library(doBy)
library(sp)
library(maptools)
## Checking rgeos availability: TRUE
if (!require(gpclib)) install.packages("gpclib", type="source")
## Loading required package: gpclib
## General Polygon Clipper Library for R (version 1.5-5)
## Type 'class ? gpc.poly' for help
gpclibPermit()
## Warning in gpclibPermit(): support for gpclib will be withdrawn from
## maptools at the next major release
## [1] TRUE
library(forcats)
library(ggmap)
##
## Attaching package: 'ggmap'
## The following object is masked from 'package:plotly':
##
## wind
library(viridis)
## Loading required package: viridisLite
library(stringi)
homicide_mex <- read_csv("perfiles_homicidios.csv")
## Parsed with column specification:
## cols(
## edo_code = col_character(),
## year = col_integer(),
## sex = col_character(),
## rango_edad = col_character(),
## escolaridad = col_character(),
## pob = col_double(),
## homtot = col_integer(),
## thom = col_double(),
## edo_nom = col_character()
## )
homicide_mex$edo_code[homicide_mex$edo_code=="01" ] <- "1"
homicide_mex$edo_code[homicide_mex$edo_code=="02" ] <- "2"
homicide_mex$edo_code[homicide_mex$edo_code=="03" ] <- "3"
homicide_mex$edo_code[homicide_mex$edo_code=="04" ] <- "4"
homicide_mex$edo_code[homicide_mex$edo_code=="05" ] <- "5"
homicide_mex$edo_code[homicide_mex$edo_code=="06" ] <- "6"
homicide_mex$edo_code[homicide_mex$edo_code=="07" ] <- "7"
homicide_mex$edo_code[homicide_mex$edo_code=="08" ] <- "8"
homicide_mex$edo_code[homicide_mex$edo_code=="09" ] <- "9"
unique(homicide_mex$edo_code)
## [1] "1" "2" "3" "4" "5" "6" "7" "8"
## [9] "9" "10" "11" "12" "13" "14" "15" "16"
## [17] "17" "18" "19" "20" "21" "22" "23" "24"
## [25] "25" "26" "27" "28" "29" "30" "31" "32"
## [33] "Todos"
getwd()
## [1] "/Users/eves/Documents/Spring 2018/Data Viz/Visual experiment"
dir_main = "/Users/eves/Documents/"
dir_adm = paste(dir_main,"Spring 2018/Data Viz/Visual experiment/mbaprgw", sep="")
mex_shp1 <- readOGR(dsn = dir_adm, layer = "mbaprgw")
## OGR data source with driver: ESRI Shapefile
## Source: "/Users/eves/Documents/Spring 2018/Data Viz/Visual experiment/mbaprgw", layer: "mbaprgw"
## with 32 features
## It has 7 fields
## Integer64 fields read as strings: COV_ COV_ID
homicide_mex$edo_code <- as.numeric(homicide_mex$edo_code)
## Warning: NAs introduced by coercion
homicide_mex$pob <- as.numeric(homicide_mex$pob)
unique(homicide_mex$edo_code)
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30 31 32 NA
homicide_mex_sub <-homicide_mex %>%
group_by(edo_code) %>%
summarise(Hom_edo = sum(homtot))
homicide_mex_sub$edo_code <- as.numeric(homicide_mex_sub$edo_code)
mex_shp2_fort <- fortify(mex_shp1, region="COV_ID") %>%
mutate(id=as.numeric(id))
map_data_hom <- mex_shp2_fort %>%
left_join(homicide_mex_sub, by = c("id"= "edo_code"))
unique(map_data_hom$Hom_edo)
## [1] 4256 46320 43416 139752 11496 80512 183840 66256 31192 20152
## [11] 63688 69208 53776 35008 7512 10952 21848 99216 42384 16696
## [21] 57448 5544 4688 55256 3664 18008 36784 10848 29464 212368
## [31] 81032
#Theme functions for maps and color map
#source: https://timogrossenbacher.ch/2016/12/beautiful-thematic-maps-with-ggplot2-only/
theme_map <- function(...) {
theme_minimal() +
theme(
text = element_text(family="Helvetica", size = 10, color = "#22211d"),
axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
#panel.grid.minor = element_line(color = "#ebebe5", size = 0.2),
panel.grid.major = element_line(color = "#ebebe5", size = 0.2),
panel.grid.minor = element_blank(),
#plot.background = element_rect(fill = "#f5f5f2", color = NA),
#panel.background = element_rect(fill = "#f5f5f2", color = NA),
#legend.background = element_rect(fill = "#f5f5f2", color = NA),
panel.border = element_blank(),
...
)
}
no_classes <- 5
labels <- c()
homm_quantiles <- quantile(map_data_hom$Hom_edo,
probs = seq(0, 1, length.out = no_classes + 1), na.rm = TRUE)
labels <- c()
for(idx in 1:length(homm_quantiles)){
labels <- c(labels, paste0(round(homm_quantiles[idx], 2),
" – ",
round(homm_quantiles[idx + 1], 2)))
}
labels <- labels[1:length(labels)-1]
map_data_hom$Hom_edo_quantiles <- cut(map_data_hom$Hom_edo,
breaks = homm_quantiles,
labels = labels,
include.lowest = T)
#colnames(homicide_mex_sub)[10] <- "name"
#homicide_mex_sub$Hom_edo <-
#map_data <- map_data_hom %>%
#left_join(homicide_mex_sub, by = c("id"= "edo_code"))
map_hom_edo<- ggplot() +
# polygons with data
geom_polygon(data = map_data_hom, aes(fill = Hom_edo_quantiles,
x = long,
y = lat,
group = group)) +
# rayon outline
geom_path(data = map_data_hom, aes(x = long,
y = lat,
group = group),
color = "grey", size = 0.1) +
# for projection
coord_equal() +
# add the previously defined basic theme + color
theme_map() +
# labels
labs(x = NULL,
y = NULL,
title = "Total Homicides ",
subtitle = "From 2011 to 2018",
caption = "Source: Data Civica") +
theme(legend.position = "bottom") +
scale_fill_viridis(
option = "magma",
discrete = T,
direction = -1,
name = "Homicide rate",
guide = guide_legend(
keyheight = unit(3, units = "mm"),
keywidth = unit(6, units = "mm"),
title.position = 'top',
reverse=F,
title.hjust = 0.5,
label.hjust = 0.5
))
ggplotly(map_hom_edo)
As we can see in the map, we can see that there are five states that present the highest number of homicides, Chiuhahua, Sinaloa, Puebla y Ciudad de Mexico. These states are traditionally in the drig dealing area, except for Mexico City and Puebla.
During this period, these states presented a high presence of “Joint Operation”. The joint operations were a security strategy in which federal security forces assisted the local police in operations against drug trafficking.
To a better understanding of the homicide evolition, it is better to transform this parameter to a rate cosideting the high population of Mexico City, its rate of homicides can be lower than other states.